<template>
  <div class="login-container" style="text-align: center">
    <div
      style="position: absolute;
    left: 0;
    right: 0;
    top:0;
    bottom:0;
    margin:auto;
    height:50%;
    width:50%"
    >
      <a-form
        ref="loginForm"
        :model="loginForm"
        class="login-form"
        auto-complete="on"
        label-position="left"
      >
        <h3 class="title">GoBGA湾区经贸通后台管理系统</h3>
        <a-form-item class="a-form-item" prop="phoneNum">
          <span class="svg-container">
            <a-icon type="user" />
          </span>
          <a-input
            placeholder="请输入账号"
            class="a-input"
            type="text"
            v-model="loginForm.phoneNum"
          />
        </a-form-item>
        <a-form-item class="a-form-item" prop="password">
          <span class="svg-container">
            <a-icon type="lock" theme="filled" />
          </span>
          <a-input
            class="a-input"
            :type="pwdType"
            v-model="password"
            name="password"
            placeholder="请输入密码"
            @keyup.enter.native="handleLogin"
          />
          <span class="show-pwd" @click="showPwd">
            <a-icon type="eye" v-if="pwdType == ''" />
            <a-icon type="eye-invisible" v-else />
          </span>
        </a-form-item>
        <a-form-item class="a-form-item">
          <a-button
            :loading="loading"
            class="a-btn"
            type="primary"
            @click.native.prevent="handleLogin"
          >
            登 录
          </a-button>
        </a-form-item>
      </a-form>
    </div>
  </div>
</template>

<script>
import api from "@/api/api.js";

export default {
  name: "Login",
  data() {
    return {
      loginForm: {
        phoneNum: "",
        password: ""
      },
      loginRules: {
        phoneNum: [{ required: true, trigger: "blur" }],
        password: [{ required: true, trigger: "blur" }]
      },
      password: "",
      loading: false,
      pwdType: "password",
      redirect: undefined,
      form: this.$form.createForm(this)
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.pwdType === "password") {
        this.pwdType = "";
      } else {
        this.pwdType = "password";
      }
    },
    handleLogin() {
      if (!this.loginForm.phoneNum) {
        this.$message.info("请输入账号");
        return;
      } else if (!this.password) {
        this.$message.info("请输入密码");
        return;
      }
      this.loading = true;
      // this.loginForm.password=this.$md5(this.password)
      this.loginForm.password = this.password;
      this.login();
    },
    //登录
    login() {
      api
        .login({
          password: this.loginForm.password,
          userName: this.loginForm.phoneNum
        })
        .then(res => {
          console.log(res);
          if (res.code == 200) {
            this.$message.success(res.description);
            sessionStorage.setItem("userId", res.result.accountId);
            sessionStorage.setItem("roleId", res.result.roleId);
            // sessionStorage.setItem("roleCode",res.result.roleCode);
            this.$router.push({ path: "/dataManage/dataList" });
          } else {
            this.$message.info(res.description);
          }
          this.loading = false;
        });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
$bg: #2d3a4b;
$light_gray: #eee;

/* reset element-ui css */
.login-container {
  .a-input {
    display: inline-block;
    height: 47px !important;
    width: 85%;
    // background: rgba(0, 0, 0, 0) !important;
    // border: 0  !important;
    background: transparent;
    border: 0px;
    -webkit-appearance: none;
    border-radius: 0px;
    padding: 12px 5px 12px 15px;
    color: $light_gray;
    box-shadow: none;
    outline: none;
    box-sizing: border-box;
    &:-webkit-autofill {
      -webkit-box-shadow: 0 0 0px 1000px $bg inset !important;
      -webkit-text-fill-color: #fff !important;
    }
  }
  .a-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
    .a-btn {
      height: 47px !important;
      width: 100%;
    }
  }
}
</style>

<style rel="stylesheet/scss" lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;
.login-container {
  position: fixed;
  height: 100%;
  width: 100%;
  background: url("../../../static/img/bg.jpg") no-repeat;
  background-color: #f5f5f5;
  background-size: cover;
  // background-color: $bg;
  .login-form {
    position: absolute;
    left: 0;
    right: 0;
    width: 520px;
    max-width: 100%;
    padding: 35px 35px 15px 35px;
    margin: 0px auto;
  }
  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;
    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }
  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }
  .title {
    font-size: 26px;
    font-weight: 400;
    color: $light_gray;
    margin: 0px auto 40px auto;
    text-align: center;
    font-weight: bold;
  }
  .show-pwd {
    // position: absolute;
    // right: 10px;
    // top: -5px;
    // font-size: 16px;
    color: $dark_gray;
    // cursor: pointer;
    // user-select: none;
  }
}
</style>
